package com.example.test_activity.fragment;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import com.example.test_activity.R;
import com.example.test_activity.http.HttpUtil;
import com.example.test_activity.utils.GetScoreEvery;
import com.example.test_activity.utils.NoDoubleClickListener;
import com.example.test_activity.viewUtil.CoordinatorMenu;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class ScoreEveryFragment extends Fragment {

    private GetScoreEvery getScoreEvery = new GetScoreEvery();

    private View view;
    private ImageView head;
    private ListView listView;
    private SimpleAdapter adapter;
    private List<Map<String, String>> list = new ArrayList<>();

    private List<Map<String, String>> list_temp = new ArrayList<>();

    private CoordinatorMenu menu;

    private Spinner spinner;
    private ImageView refresh;
    private int flag = 0;

    private String html_no;
    private String html_all;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.view = inflater.inflate(R.layout.fragment_score_every, container, false);
        init();
        onClickSpinner();

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        head.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (menu.isOpened()) {
                    menu.closeMenu();
                } else {
                    menu.openMenu();
                }
            }
        });

        refresh.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                getScore();
            }
        });
    }

    private void init() {
        menu = (CoordinatorMenu) getActivity().findViewById(R.id.menu);
        head = (ImageView) view.findViewById(R.id.fg_head);
        refresh = (ImageView) view.findViewById(R.id.fg_refresh);

        spinner = (Spinner) view.findViewById(R.id.spinner);

        listView = (ListView) view.findViewById(R.id.list_score_every);
        adapter = new SimpleAdapter(getContext(), list, R.layout.score_item,
                new String[]{"name", "credit", "score", "reason"},
                new int[]{R.id.tv_name, R.id.tv_credit, R.id.tv_score, R.id.tv_reason});
        listView.setAdapter(adapter);
    }

    //全部及格成绩
    private void getOk() {

        list.clear();

        list_temp.clear();
        list_temp = getScoreEvery.getScoreOk(html_all);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
    }

    //现在不及格
    private void getNow() {
        list.clear();

        list_temp.clear();
        list_temp = getScoreEvery.getScoreNow(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
    }

    //曾经不及格
    private void getAgo() {

        list.clear();

        list_temp.clear();
        list_temp = getScoreEvery.getScoreAgo(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }

    }

    private void getAll() {
        list.clear();

        list_temp.clear();
        list_temp = getScoreEvery.getScoreOk(html_all);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
        list_temp.clear();
        list_temp = getScoreEvery.getScoreAgo(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
        list_temp.clear();
        list_temp = getScoreEvery.getScoreNow(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
    }

    private void getSelect() {
        list.clear();

        list_temp.clear();
        list_temp = getScoreEvery.getScoreOk(html_all);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
        list_temp.clear();
        list_temp = getScoreEvery.getScoreAgo(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
        list_temp.clear();
        list_temp = getScoreEvery.getScoreNow(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }

        List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
        for (Map<String, String> map : list) {
            if (map.get("properties").equals("任选")) {
                temp.add(map);
            }
        }
        list.clear();
        list.addAll(temp);

    }

    private void getMust() {
        list.clear();

        list_temp.clear();
        list_temp = getScoreEvery.getScoreOk(html_all);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
        list_temp.clear();
        list_temp = getScoreEvery.getScoreAgo(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }
        list_temp.clear();
        list_temp = getScoreEvery.getScoreNow(html_no);
        if (!list_temp.isEmpty()) {
            list.addAll(list_temp);
        }

        List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
        for (Map<String, String> map : list) {
            if (map.get("properties").equals("必修")) {
                temp.add(map);
            }
        }
        list.clear();
        list.addAll(temp);
    }

    private void onClickSpinner() {
        String[] mItem = getResources().getStringArray(R.array.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, mItem);
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setSelection(0);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                flag = position;
                getScore();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void onclick(int position) {
        switch (position) {
            case 0:
                getAll();
                break;
            case 1:
                getMust();
                break;
            case 2:
                getSelect();
                break;
            case 3:
                getOk();
                break;
            case 4:
                getNow();
                break;
            case 5:
                getAgo();
                break;
        }
    }

    private void sendRequest()
    {
        try {
            html_no = HttpUtil.getInstance().sendGetPost().getScore_no().execute().body().string();
            html_all = HttpUtil.getInstance().sendGetPost().getScore_all().execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getScore() {

        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                sendRequest();
                e.onNext("开始");
                e.onComplete();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String string) {
                        onclick(flag);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        adapter.notifyDataSetChanged();
                    }
                });
    }
}
